package cn.org.guhao.practice.go.util;

import cn.org.guhao.practice.go.model.WordCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 字符串工具类
 * @author guhao
 * @since 2019-12-05
 */
public class StringUtils {

  private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);

  private StringUtils(){
    logger.error("[{}]工具类保护",StringUtils.class.getName());
  }

  public static final String BLANK = " ";
  public static final String EMPTY = "";

  /**
   * 匹配格式
   * @param pattern
   * @param str
   * @return
   */
  public static boolean match(String pattern,String str){
    String[] stringArray = str.split(" ");
    if(stringArray.length != pattern.length()){
      return false;
    }
    Map<String,String> p2s = new HashMap<String, String>();
    for (int i=0;i<pattern.length();i++){
      String p = String.valueOf(pattern.charAt(i));
      String ps = stringArray[i];
      if(p2s.containsKey(p)){
        if(!ps.equals(p2s.get(p))){
          return false;
        }
      }else {
        p2s.put(p,ps);
      }
    }
    return true;
  }

  /**
   * 统计单词
   * @param str
   * @return
   */
  public static TreeMap<String,Integer> countWords(String str){
    String[] words = str.split(BLANK);
    TreeMap<String,Integer> counts = new TreeMap<String, Integer>();
    for (String word:words){
      if(word == null || word.trim().equals(EMPTY)){
        continue;
      }
      int count = 1;
      word = word.toLowerCase();
      if(counts.containsKey(word)){
        count = counts.get(word) + 1;
      }
      counts.put(word,count);
    }
    return counts;
  }

  public static Map<String,Integer> countWords(String[] words){
    Map<String,Integer> counts = new HashMap<String, Integer>();
    for (String word:words){
      if(word == null || word.trim().equals(EMPTY)){
        continue;
      }
      int count = 1;
      word = word.toLowerCase();
      if(counts.containsKey(word)){
        count = counts.get(word) + 1;
      }
      counts.put(word,count);
    }
    return counts;
  }

  public static List<WordCount> countWords(List<String> words){
    List<WordCount> wordCountList = new ArrayList<>();
    Map<String,WordCount> counts = new HashMap<>();
    for (String word:words){
      if(word == null || word.trim().equals(EMPTY)){
        continue;
      }
      WordCount wordCount;
      if(counts.containsKey(word)){
        wordCount = counts.get(word);
        wordCount.setCount(wordCount.getCount() + 1);
      } else {
        wordCount = new WordCount(word);
        wordCountList.add(wordCount);
      }
      counts.put(word,wordCount);
    }
    return wordCountList;
  }

  /**
   * 查找第一个唯一的字符
   * @param str
   * @return
   */
  public static String firstOnceChar(String str){
    // 思路一
    char[] charArray = str.toCharArray();
    for (int i=0;i<charArray.length;i++){
      String once = null;
      for(int j=0;j<charArray.length;j++){
        if (charArray[i] == charArray[j] && i != j){
          once = null;
          break;
        }
        once = String.valueOf(charArray[i]);
      }
      if(once != null){
        return once;
      }
    }
    // 思路二：LinkedHashMap统计
    return null;
  }

  /**
   * 查找重复字符串
   * @param list
   * @return
   */
  public static Set<String> findRepeatString(Collection<String> list){
    Set<String> set = new HashSet<String>();
    Set<String> stringSet = new HashSet<String>();
    for (String str:list){
      if(set.add(str)){
        continue;
      }
      stringSet.add(str);
    }
    return stringSet;
  }

  /**
   * 查找重复单词
   * @param words
   * @return
   */
  public static Set<String> findRepeatWords(String[] words){
    Set<String> stringSet = new HashSet<String>();
    Map<String,Integer> counts = countWords(words);
    for(Map.Entry<String,Integer> entry:counts.entrySet()){
      if(entry.getValue() > 1){
        stringSet.add(entry.getKey());
      }
    }
    return stringSet;
  }

}
